/*
 * @Descripttion: unTitle
 * @Author: yizheng.yuan
 * @Date: 2021-10-28 18:32:08
 * @LastEditors: yizheng.yuan
 * @LastEditTime: 2021-11-05 09:22:26
 */
export default {
  isPhone(phone){
    const reg=/^1[3-9]{1}\d{9}$/;
    console.log('phone',phone,reg.test(phone));
    return reg.test(phone);
  },
  cookieSet: function (key, val, time) {//设置cookie方法
    var date = new Date(); //获取当前时间
    var expiresDays = time;  //将date设置为n天以后的时间
    date.setTime(date.getTime() + expiresDays * 24 * 3600 * 1000); //格式化为cookie识别的时间
    document.cookie = key + "=" + val + ";expires=" + date.toGMTString();  //设置cookie
  },
  cookieGet: function (key) {//获取cookie方法
    /*获取cookie参数*/
    var getCookie = document.cookie.replace(/[ ]/g, "");  //获取cookie，并且将获得的cookie格式化，去掉空格字符
    var arrCookie = getCookie.split(";")  //将获得的cookie以"分号"为标识 将cookie保存到arrCookie的数组中
    var tips;  //声明变量tips
    for (var i = 0; i < arrCookie.length; i++) {   //使用for循环查找cookie中的tips变量
      var arr = arrCookie[i].split("=");   //将单条cookie用"等号"为标识，将单条cookie保存为arr数组
      if (key == arr[0]) {  //匹配变量名称，其中arr[0]是指的cookie名称，如果该条变量为tips则执行判断语句中的赋值操作
        tips = arr[1];   //将cookie的值赋给变量tips
        break;   //终止for循环遍历
      }
    }
    return tips;
  },
  getData({ url, data, that }) {
    return new Promise((resolve, reject) => {
      that.$axios.get(url, {
        params: data
      })
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  disableScale() {
    // 禁用缩放
    function addMeta() {
      document.getElementsByTagName('head')[0].append('<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />');
    }
    setTimeout(addMeta, 3000);

    // 禁用双指放大
    document.documentElement.addEventListener('touchstart', function (event) {
      if (event.touches.length > 1) {
        event.preventDefault();
      }
    }, {
      passive: false
    });

    // 禁用双击放大
    var lastTouchEnd = 0;
    document.documentElement.addEventListener('touchend', function (event) {
      var now = Date.now();
      if (now - lastTouchEnd <= 300) {
        event.preventDefault();
      }
      lastTouchEnd = now;
    }, {
      passive: false
    });
  },
  goBottom() {
    //滚动条在Y轴上的滚动距离

    function getScrollTop() {
      var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
      if (document.body) {
        bodyScrollTop = document.body.scrollTop;
      }
      if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
      }
      scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
      return scrollTop;
    }

    //文档的总高度

    function getScrollHeight() {
      var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
      if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
      }
      if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
      }
      scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
      return scrollHeight;
    }

    //浏览器视口的高度

    function getWindowHeight() {
      var windowHeight = 0;
      if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
      } else {
        windowHeight = document.body.clientHeight;
      }
      return windowHeight;
    }

    window.onscroll = function () {
      if (getScrollTop() + getWindowHeight() == getScrollHeight()) {
        alert("已经到最底部了！!");
      }
    };
  },
  // 毫数转格式 【yyyy-MM-dd】
  formatDate(time, format = 'yyyy-MM-dd HH:mm:ss') {
    const t = new Date(time);
    const tf = function (i) { return (i < 10 ? '0' : '') + i; };
    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
      switch (a) {
        case 'yyyy':
          return tf(t.getFullYear());
        // break;
        case 'MM':
          return tf(t.getMonth() + 1);
        // break;
        case 'mm':
          return tf(t.getMinutes());
        // break;
        case 'dd':
          return tf(t.getDate());
        // break;
        case 'HH':
          return tf(t.getHours());
        // break;
        case 'ss':
          return tf(t.getSeconds());
        // break;
      };
    });
  }
}